package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.common.util.ExcelUtil;
import com.recharge.common.util.TemplateUtils;
import com.recharge.domain.query.TransactionQueryDO;
import com.recharge.domain.vo.TransactionDO;
import com.recharge.service.TransactionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 交易信息表 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/transaction")
public class TransactionController {

    @Value("${file.transaction.template}")
    private String fileTemplatePath;

    @Autowired
    TransactionService transactionService;

    @RequiresPermissions("transaction:page")
    @RequestMapping(value = "/page.html", method = RequestMethod.POST)
    @ApiOperation(value = "查询交易记录")
    public @ResponseBody
    ObjectResponse page(@RequestBody TransactionQueryDO domain) {
        ObjectResponse response = new ObjectResponse();

        ResultDO result = transactionService.find(domain);
        if (result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("transaction:get")
    @RequestMapping(value = "/get.html", method = RequestMethod.POST)
    @ApiOperation(value = "获取交易信息", notes = "")
    public @ResponseBody
    ObjectResponse<TransactionDO> get(@RequestBody TransactionQueryDO domain) {

        ObjectResponse<TransactionDO> response = new ObjectResponse<>();

        ResultDO result = transactionService.get(domain.getId());

        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((TransactionDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("transaction:excel-download")
    @RequestMapping(value = "/excelDownload.html", method = RequestMethod.POST)
    @ApiOperation(value = "excel文件导出提现记录", notes = "")
    public void excelDownload(@RequestBody TransactionQueryDO query, HttpServletResponse response) {

        String[] amountTypes = {"未知", "收入", "支出"};
        String[] types = {"未知", "存款", "提现", "商品订单", "退款", "固定授信", "临时款还款"};
        String[] statuses = {"待审核", "审核通过", "审核驳回"};
        ResultDO result = transactionService.find(query);
        PageDO<TransactionDO> lists = (PageDO<TransactionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        List<TransactionDO> list = lists.getData();
        List<Object> downloadList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> data = new HashMap<>();
            data.put("a1", item.getTransactionNo());
            data.put("a2", item.getMerchantName());
            data.put("a3", item.getGoodsName());
            data.put("a4", item.getAmount() / 1000);
            data.put("a5", amountTypes[item.getAmountType()]);
            data.put("a6", (!TemplateUtils.isEmpty(item.getBeforeBalance())) ? item.getBeforeBalance() / 1000 : "");
            data.put("a7", (!TemplateUtils.isEmpty(item.getBalance())) ? item.getBalance() / 1000 : "");
            data.put("a8", types[item.getType()]);
            data.put("a9", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getBeginDate()));
            data.put("a10", statuses[item.getStatus()]);
            downloadList.add(data);
        });
        ExcelUtil.exportExcel(response, downloadList, fileTemplatePath, "交易记录列表.xls");

    }
}
